package com.surrey.p2p.view;

import java.util.Observable;
import java.util.Observer;

import com.surrey.p2p.model.CANNode;
import com.surrey.p2p.model.P2PNode;

/**
 * Network to observe all the nodes and visualise changes to the user.
 * No side effects to the node objects allowed through this class
 * 
 * @author Vedika Dalmia
 *
 */

public class Network implements Observer {
	
	P2PNetworkRenderer networkRenderer;
	
	public Network(){
		networkRenderer=new P2PNetworkRenderer();
		networkRenderer.drawGraph();
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		if (arg1 instanceof String) {
			String name = (String) arg1;
			P2PNode n = (P2PNode) arg0;
			if (name.equalsIgnoreCase("online")) {
				//System.out.println("Node " + n.getAddress() + " is now online");
				networkRenderer.addNodeToVisualisation((CANNode) n);
			}
			else{
			if (name.equalsIgnoreCase("offline")) {
				//System.out.println("Node " + n.getAddress() + " is now offline");
				networkRenderer.addNodeToVisualisation((CANNode) n);
			}
			else{
			if(name.startsWith("route")){
				//System.out.println("Routing from " + n.getAddress() + " to " + name.substring(5, name.length()));
			    networkRenderer.displayRouting(n.getAddress()+  name.substring(6, name.length()),n);
			}
			else{
				if(name.equals("routing complete")){
					//System.out.println("Routing complete");
					networkRenderer.finishRouting(n);
				}
			}
			}
			}
		}

	}
}
